package com.oracle.zebrafreelytraveling.net;

/**
 * Created by yydcnjjw on 2017/7/16.
 */

import android.app.FragmentManager;
import android.net.Uri;
import android.util.Log;

import com.oracle.zebrafreelytraveling.data.RequestResult.ResState;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResViewSpotIntro;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResViewSpotList;
import com.oracle.zebrafreelytraveling.data.model.IntroBase;
import com.oracle.zebrafreelytraveling.ui.fragment.IntroSlideDialogFragment;
import com.oracle.zebrafreelytraveling.utils.FileUtil;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.oracle.zebrafreelytraveling.utils.StringUtil;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络请求管理
 */

public class HttpManager {

    private static HttpManager httpManager;

    private HttpManager() {
    }

    /**
     * 单例
     */
    public static HttpManager getInstance() {
        synchronized (HttpManager.class) {
            if (httpManager == null) {
                httpManager = new HttpManager();
            }
        }
        return httpManager;
    }

    public Api getService(String baseUrl) {

        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(15, TimeUnit.SECONDS);
        builder.connectTimeout(10, TimeUnit.SECONDS);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(builder.build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(Api.class);
    }

    public static void updateUserInfo(String userjson) {
        getInstance().getService(Constant.BASE_URL)
                .updateUserInfo(userjson)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResState>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }
                    @Override
                    public void onNext(@NonNull ResState resState) {
                        ToastUtil.showToast(resState.getMessage());
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("errorupdate", e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                    }
                });

    }

    //上传用户的一些大文件
    public static void uploadUserInfo(String uri) throws IOException {
        File file = FileUtil.getFileByUri(Uri.parse(uri));
        Log.d("urlFile", file.getAbsolutePath());

        // 创建 RequestBody，用于封装构建RequestBody
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", StringUtil.getFileName(uri), requestFile);
        Log.d("contentLength", ""+requestFile.contentLength());

        getInstance().getService(Constant.BASE_URL)
                .uploadFile(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResState>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResState resState) {
                        Log.d("resState", resState.getCode());
                        ToastUtil.showToast(resState.getMessage());

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("errorupload", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    public static void addFriend(int friendId, int userId) {
        getInstance().getService(Constant.BASE_URL)
                .addFriend(userId, friendId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResState>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResState resState) {
                        ToastUtil.showToast(resState.getMessage());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("addFriend", e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
